package se.jocke.nb.log.model;

import com.google.common.primitives.Ints;
import java.awt.Color;
import java.awt.Font;
import java.util.regex.Pattern;

/**
 *
 * @author jocke
 */
public class LogViewFilter implements Comparable<LogViewFilter> {

    public static final String DEFAULT_FILTER_NAME = "default";

    private String name;

    private String pattern;

    private Font font;

    private Color foregroundColor;

    private Color backgroundColor;

    private boolean regex;

    private int weight;
    

    private Pattern regexPattern;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public Font getFont() {
        return font;
    }

    public void setFont(Font font) {
        this.font = font;
    }

    public Color getForegroundColor() {
        return foregroundColor;
    }

    public void setForegroundColor(Color foregroundColor) {
        this.foregroundColor = foregroundColor;
    }

    public Color getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public boolean isRegex() {
        return regex;
    }

    public void setRegex(boolean regex) {
        this.regex = regex;
    }
    
    
    public boolean isDefault() {
        return DEFAULT_FILTER_NAME.equalsIgnoreCase(name);
    }

    public boolean matches(String line) {

        if (isDefault()) {
            return true;

        } else {

            if (regexPattern == null && isRegex()) {
                regexPattern = Pattern.compile(pattern);
            }

            if (isRegex()) {
                return regexPattern.matcher(line).matches();

            } else {
                return line.contains(pattern);
            }
        }

    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final LogViewFilter other = (LogViewFilter) obj;
        return !((this.name == null) ? (other.name != null) : !this.name.equals(other.name));
    }

    @Override
    public String toString() {
        return "Filter{" + "name=" + name + ", pattern=" + pattern + ", font=" + font + ", foregroundColor=" + foregroundColor + ", backgroundColor=" + backgroundColor + '}';
    }

    @Override
    public int compareTo(LogViewFilter o) {
        return Ints.compare(weight, o.weight);
    }


}
